<?php
class Zendvn_View_Helper_TinyMce extends Zend_View_Helper_Abstract
{
	
	public function tinyMce($name,$value = null, $attribs = null, $options = null){
		
		$jsUrl = SCRIPTS_URL . '/tinymce/tiny_mce.js';
		$tinyPath = SCRIPTS_PATH . '/tinymce';
		unset($_SESSION['ajaxFile']);	
		$ajaxArrary = array('member'=>true);
		$_SESSION['ajaxFile'] = $ajaxArrary;
		$strAttribs = '';
		if(count($attribs)>0){
			foreach ($attribs as $key => $val){
				$strAttribs .= $key . '="' . $val . '" ';
			}			
		}
		
		$this->view->headScript()->appendFile($jsUrl);
		if($options == null){
			$script = '
				tinyMCE.init({
				        mode : "exact",
	        			theme : "advanced",       
	        			elements :"' . $name . '", 		
	        			entity_encoding : "raw",//Hien thi dung dinh dang tieng viet
	        			plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
	        			//  plugins : "safari,advlink,imagemanager",
	        			 // Skin options
				        skin : "o2k7",
				        skin_variant : "silver",
				        
				         // Theme options
				        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
				        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
				        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
				        //theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
				        theme_advanced_toolbar_location : "top",
				        theme_advanced_toolbar_align : "left",
				        theme_advanced_statusbar_location : "bottom",
				        file_browser_callback : "ajaxfilemanager",
				        //theme_advanced_resizing : false
					});
					
					function ajaxfilemanager(field_name, url, type, win) {
						var ajaxfilemanagerurl = "' . SCRIPTS_URL . '/tinymce/plugins/ajaxfilemanager/ajaxfilemanager.php";
						var view = "detail";
						switch (type) {
							case "image":
							view = "thumbnail";
								break;
							case "media":
								break;
							case "flash": 
								break;
							case "file":
								break;
							default:
								return false;
					}
		            tinyMCE.activeEditor.windowManager.open({
		                url: "' . SCRIPTS_URL . '/tinymce/plugins/ajaxfilemanager/ajaxfilemanager.php?view=" + view,
		                width: 782,
		                height: 500,
		                inline : "yes",
		                close_previous : "no"
		            },{
		                window : win,
		                input : field_name
		            });
		            }
					';
		}
		
		if($options['task'] == 'account'){
			$script = '
			tinyMCE.init({
			mode : "exact",
			theme : "advanced",
			elements :"' . $name . '",
			entity_encoding : "raw",//Hien thi dung dinh dang tieng viet
			plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
			//  plugins : "safari,advlink,imagemanager",
			// Skin options
			skin : "o2k7",
			skin_variant : "silver",
			
			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect,code",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			//theme_advanced_statusbar_location : "bottom",
			file_browser_callback : "ajaxfilemanager",
			//theme_advanced_resizing : false
			});
				
			function ajaxfilemanager(field_name, url, type, win) {
			var ajaxfilemanagerurl = "' . SCRIPTS_URL . '/tinymce/plugins/ajaxfilemanager/ajaxfilemanager.php";
			var view = "detail";
			switch (type) {
			case "image":
			view = "thumbnail";
			break;
			case "media":
			break;
			case "flash":
			break;
			case "file":
			break;
			default:
			return false;
			}
			tinyMCE.activeEditor.windowManager.open({
			url: "' . SCRIPTS_URL . '/tinymce/plugins/ajaxfilemanager/ajaxfilemanager.php?view=" + view,
			width: 782,
			height: 500,
			inline : "yes",
			close_previous : "no"
			},{
			window : win,
			input : field_name
			});
			}
			';
		}
		
		$this->view->headScript()->appendScript($script);
		$xhtml = '<textarea id="' . $name . '" name="' . $name . '" ' . $strAttribs . ' >'
				  . $value
				   . '</textarea>';
		
		return $xhtml;
	}
}
